
using System;
using System.Linq;
using System.Collections.Generic;
using BLToolkit.DataAccess;
using BLToolkit.Mapping;
using BLToolkit.Data.Linq;
namespace Core
{
    [TableName("tblCanBo_ChucVu")]
    public class CanBoChucVu
    {
        
        public int MaCanBo { get; set; }
        public int MaChucVu { get; set; }

        // FK_tblCB_CV_tblcanbo
        [Association(ThisKey = "MaCanBo", OtherKey = "MaCanBo", CanBeNull = false)]
        public CanBo CanBo { get; set; }
        // FK_tblCB_CV_tblchucvu
        [Association(ThisKey = "MaChucVu", OtherKey = "MaChucVu", CanBeNull = false)]
        public ChucVu ChucVu { get; set; }

        public static IQueryable<CanBoChucVu> CanBoChucVus
        {
            get
            {
                using (var db = new DataContext())
                {
                    return db.GetTable<CanBoChucVu>();
                }
            }
        }
        public static List<CanBoChucVu> All()
        {
            return CanBoChucVus.ToList();
        }

        public static int Add(CanBoChucVu entity)
        {
            using (var db = new DataContext())
            {
                var exists = CanBoChucVus.SingleOrDefault(x => x.MaCanBo == entity.MaCanBo && x.MaChucVu == entity.MaChucVu);
                if (exists == null)
                    return db.Insert(entity);
                return -1;
            }
        }
        public static int Update(CanBoChucVu entity)
        {
            using (var db = new DataContext())
            {
                return db.Update(entity);
            }
        }
        public static int Delete(int maCanBo, int maChucVu)
        {
            return CanBoChucVus.Delete(x => x.MaCanBo == maCanBo && x.MaChucVu == maChucVu);
        }
    }
}